﻿using DDD.Domain.Abstract.DomainEvent;
using DDD.Domain.DomainEvents.UserDomainEvents;
using DDD.IntegrationEvent;
using MassTransit;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using RabbitMQ.Client.Exceptions;

namespace DDD.Application.DomainEventHandlers
{
    public sealed class CreateUserDomainEventHandler : IDomainEventHandler<CreateUserDomainEvent>
    {
        private readonly ILogger<CreateUserDomainEventHandler> _logger;
        private readonly IPublishEndpoint _publishEndpoint;

        public CreateUserDomainEventHandler(ILogger<CreateUserDomainEventHandler> logger, IPublishEndpoint publishEndpoint)
        {
            _logger = logger;
            _publishEndpoint = publishEndpoint;
        }

        public async Task Handle(CreateUserDomainEvent notification, CancellationToken cancellationToken)
        {
            await _publishEndpoint.Publish<CreateUserIntegrationEvent>(notification.ToCreateUserIntegrationEvent());
        }
    }
}
